% GAUTIER LE BIHAN - 2020
% Replication files for “Shocks vs Menu Costs: Patterns of Price Rigidity in an Estimated Multi-Sector Menu-Cost Model�? –Review of Economics and Statistics
%
% Estimates Cplus model +leptokurtic shock with SMM for each secto in turn 
% Produce input for table 3 Panel D

clear all
close all
clear matrix
clc
addpath('..\..\Utilities')  
load actual_moments_k


load stat_tempb
param0=stat_tempb(:,2:5);

   
        
for jj=1:227;

   p00 =abs(param0(jj,1));%param0(jj,1);  
    mu_c0 =param0(jj,2);%pa&ram0(jj,2)+param0(jj,2)/5 ;
    sig_eps_a0=param0(jj,3);%param0(jj,3);
    p_a0 =param0(jj,4);%param0(jj,4);
      
     
weight_j=actual_moments_k(jj,12);
weight_j
%0.050v0    0.0554    0.0455    0.5816
p0_init = p00;
mu_c_init = mu_c0;
sig_eps_a_init = sig_eps_a0;
p_a_init = p_a0;


% Adjustement of initial search area (beyond 5 percent)
power_p0 = 1;
power_mu_c = 1;
power_sig_eps_a = 1;
power_p_a =1;

% save SMM

% create vector for initial parameter values
vec0 = [p00; mu_c0; sig_eps_a0; p_a0]

% Keep track of results
conv_SMM = zeros(0,3);
xopt_SMM = 100000; % start iteration
conv_opt = zeros(3,6);
SMM_count = 0; % counter

% tolerance level
tol_SMM = 5e-3;



save('SMM','vec0','p00','mu_c0','sig_eps_a0','p_a0',...
    'p0_init','mu_c_init','sig_eps_a_init','p_a_init',...
    'power_p0','power_mu_c','power_sig_eps_a','power_p_a',...
    'conv_SMM','xopt_SMM','conv_opt','SMM_count',...
    'tol_SMM','-append', 'weight_j')
% 'power_phi','phi_init','phi0'

%% Options
%  Display, TolX, TolFun, MaxFunEvals

%% Set calibration target
% std.Y, autocorr(Y), frequency of default:
moment1 = actual_moments_k(jj,2);
moment2 = actual_moments_k(jj,3);
moment3 = actual_moments_k(jj,4);
moment4 = actual_moments_k(jj,5); 
moment5 = actual_moments_k(jj,6);

target = [moment1; moment2; moment3; moment4; moment5];
scale=[actual_moments_k(jj,7:11)'];
scale=scale.*scale*1000
save('SMM','target', 'scale','-append')
%% Start minimization routine 
options = optimset('MaxFunEvals',50);
[x,fval, exiflag] = fminsearch(@SMMmain,vec0, options);
stat_tempb(jj,:)=[jj  x' fval exiflag target' ]

 save stat_tempb stat_tempb;
   end


 
   
   

